"""
创建生产者和消费者类实现线程间通信
"""
import threading
import queue


class Producer(threading.Thread):
    """
    生产者类
    """
    def __init__(self, name, q):
        super().__init__(name=name)
        self.queue = q

    def run(self):
        """
        重写run方法
        """
        for i in range(1, 6):
            print(f'生产者生产了{i}号产品')
            self.queue.put(i)


class Consumer(threading.Thread):
    """
    消费者类
    """
    def __init__(self, name, q):
        super().__init__(name=name)
        self.queue = q

    def run(self):
        """
        重写run方法
        """
        for i in range(1, 6):
            print(f'消费者消费了{self.queue.get()}号产品')


if __name__ == '__main__':
    queue = queue.Queue(3)
    producer = Producer('生产者', queue)
    consumer = Consumer('消费者', queue)
    producer.start()
    consumer.start()
    producer.join()
    consumer.join()
    print('程序执行完毕')
